package com.xneure.database.sql;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import com.xneure.database.annotation.DBField;
import com.xneure.database.sql.handler.HandlerType;
import com.xneure.database.sql.handler.SqlHandler;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.SpringUtil;

public class SqlMapping{
	public static SqlHandler getHandler(HandlerType hType,Class<?> cls){
		Map<String,SqlHandler> handlers = SpringUtil.getBeanByCls(SqlHandler.class);
		if(null != handlers){
			for(SqlHandler sh:handlers.values()){
				if(sh.isSupport(hType,cls)){
					return sh;
				}
			}
		}
		return hType.getHandler();
	}

	public Class<?> getTargetType(Field f,DBField anno){
		Class<?> targetType;
		if(null == anno){
			targetType = f.getType();
		}else{
			targetType = anno.targetType();
		}
		if(targetType.equals(List.class)){
			targetType = ObjectUtil.getTargetType(f);
		}
		return targetType;
	}
}
